home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / nn.zip / REROUTE.C < prev    next >
C/C++ Source or Header  |  1989-06-28  |  8KB  |  408 lines

  1. #include "config.h"
  2.  
  3. #define TRACE
  4.  
  5.  
  6. #ifdef HAVE_ROUTING
  7.  
  8. reroute(route, address)
  9. char *route, *address;
  10. {
  11.     char *name, *atpos;
  12.     register char *sp;
  13.     register c;
  14.     
  15.     if (atpos = strchr(address, '@')) {
  16.     name = atpos;
  17.     
  18.     while (--name >= address)
  19.         if (isspace(*name) || *name == '<') { 
  20.         name++;
  21.         break;
  22.         }
  23.     if (name < address) name++;
  24.     
  25.     for (sp = atpos; c = *sp; sp++) 
  26.         if (isspace(c) || c == '>') break;
  27.  
  28.     *sp = NUL;
  29.     strcpy(route, name);
  30.     *sp = c;
  31.     } else 
  32.     strcpy(route, address);
  33.     return 1;
  34. }
  35.  
  36. #else
  37.  
  38. #ifndef HAVE_GETHOSTNAME
  39.  
  40. #ifdef HAVE_UNAME
  41.  
  42. #include <sys/utsname.h>
  43.  
  44. gethostname(name, length)
  45. char *name;
  46. int length;
  47. {
  48.     struct utsname utsname;
  49.     char *host;
  50.     
  51.     uname(&utsname);
  52.     strncpy(name, utsname.nodename, length);
  53. }
  54.  
  55.  
  56. #else
  57.  
  58. #ifdef HOSTNAME
  59.  
  60. gethostname(name, length)
  61. char *name;
  62. int length;
  63. {
  64.     strncpy(name, HOSTNAME, length);
  65. }
  66.  
  67. #else
  68.  
  69. YOU LOOSE ON GETHOSTNAME
  70.  
  71. #endif /* HOSTNAME */
  72.  
  73. #endif /* HAVE_UNAME */
  74.  
  75. #endif /* HAVE_GETHOSTNAME */
  76.  
  77.  
  78. #ifdef TRACE
  79. FILE *route_trace = NULL;
  80. #endif
  81. static char cmdc;    /* we need this for trace output */
  82.  
  83. reroute(route, address)
  84. char *route, *address;
  85. {
  86.     char *name, *site, *domain;
  87.     char *atpos, *dotpos;
  88.     register char *sp;
  89.     register c;
  90.     int found;
  91.     
  92. #ifdef TRACE
  93.     if (route_trace || 
  94.     (route_trace = open_file(relative(lib_directory, "trace"),
  95.                 OPEN_APPEND | DONT_CREATE)))
  96.     fprintf(route_trace, "--orig: '%s'\n", address);
  97. #endif
  98.  
  99.     found = 0;
  100.     
  101.     /* if a sender (or receiver!) is not provided,
  102.      * we first try the site from the 'Reply-To:'
  103.      * and 'From:' lines  who@site.domain */
  104.     
  105.     if (atpos = strchr(address, '@')) {
  106.     *atpos = NUL;
  107.     name = atpos;
  108.     
  109.     while (--name >= address)
  110.         if (isspace(*name) || *name == '<') { 
  111.         name++;
  112.         break;
  113.         }
  114.     if (name < address) name++;
  115.     
  116.     dotpos = atpos;
  117.     site   = atpos + 1;
  118.  
  119.      next_dot:
  120.     *dotpos = NUL;
  121.     domain = dotpos + 1;
  122.     for (sp = domain; c = *sp; sp++) {
  123.         if (isspace(c) || c == '>') break;
  124.         if (c == '.') {
  125.         *dotpos = '.';
  126.         dotpos = sp;
  127.         goto next_dot;
  128.         }
  129.     }
  130.     *sp = NUL;
  131.     if (site == domain)
  132.         domain = NULL;
  133.     else
  134.         *atpos = NUL;    /* overwritten when first . is found */
  135.  
  136. #ifdef TRACE
  137.     if (route_trace)
  138.         fprintf(route_trace,
  139.             "   @-type: name='%s' site='%s' domain='%s'\n",
  140.             name, site, domain ? domain : "");
  141. #endif
  142.     
  143.     found = find_route(route, name, site, domain, (char *)NULL);
  144.     
  145.     if (dotpos) { *dotpos = '.'; *sp = c; }
  146.     if (atpos) *atpos = '@';
  147.  
  148.     goto out;
  149.     }
  150.  
  151.     /*
  152.      * not domain address -- look for bang address
  153.      */
  154.  
  155.     if (!(name = strrchr(address, '!'))) {
  156.     /*
  157.      * neither domain nor bang -- suppose it is a local address
  158.      */
  159.     strcpy(route, address);
  160.     found = 1;
  161.     goto out;
  162.     }
  163.     
  164.     *name++ = NUL;
  165.     if (site = strrchr(address, '!'))
  166.     *site++ = NUL;
  167.     else {
  168.     site = address;
  169.     address = NULL;
  170.     }
  171.     
  172. #ifdef TRACE
  173.     if (route_trace)
  174.     fprintf(route_trace,
  175.         "   !-type: name='%s' site='%s' bang='%s'\n",
  176.         name, site, address ? address : "NONE");
  177. #endif
  178.     
  179.     found = find_route(route, name, site, (char *)NULL, address);
  180.     
  181.     *--name = '!';
  182.     if (address) *--site = '!';
  183.  
  184.  out:
  185.  
  186. #ifdef TRACE
  187.     if (route_trace) {
  188.     if (found)
  189.         fprintf(route_trace, "--route='%s'\n\n", route);
  190.     else
  191.         fprintf(route_trace, "--NO ROUTE\n\n");
  192.     fclose(route_trace);
  193.     route_trace = NULL;
  194.     }
  195. #endif    
  196.     return found;
  197. }
  198.  
  199.  
  200. static char *cstreq(string, match)
  201. char *string, *match;
  202. {
  203.     register char *s1, *s2;
  204.     s1 = string;
  205.     
  206. next_part:
  207.     s2 = match;
  208.     
  209.     while (isspace(*s1) || *s1 == ',') s1++;
  210.  
  211.     while (*s2) {
  212.     if (*s1 == NUL || isspace(*s1)) return NULL;
  213.     if (*s1 == ',') goto next_part;
  214.     if (toupper(*s1) != toupper(*s2)) break;
  215.     s1++, s2++;
  216.     }
  217.     
  218.     if (*s2 == NUL && (*s1 == NUL || isspace(*s1) || *s1 == ',')) {
  219.     if (*s1 == ',') while (*s1 && !isspace(*s1)) s1++;
  220. #ifdef TRACE        
  221.     if (route_trace) 
  222.         fprintf(route_trace, "/%c %s=%s -> %s", cmdc, string, match, s1);
  223. #endif        
  224.     return s1;
  225.     }
  226.     
  227.     while (*s1 && !isspace(*s1)) {
  228.     if (*s1 == ',') goto next_part;
  229.     s1++;
  230.     }
  231.     
  232.     return NULL;
  233. }
  234.  
  235.  
  236. static char *cstrcpy(s1, s2)
  237. register char *s1, *s2;
  238. {
  239.     while (*s2 && isspace(*s2)) s2++;
  240.     while (*s2 && !isspace(*s2) && *s2 != ',') *s1++ = *s2++;
  241.     *s1 = NUL;
  242.     return s1;
  243. }
  244.  
  245. /*
  246.  * lookup site,domain in routes database
  247.  * if not found and bang is non-empty, use bang default if it exist
  248.  */
  249.  
  250. static find_route(route, remote_user, remote_host, remote_domain, bang)
  251. char *route, *remote_user, *remote_host, *remote_domain, *bang;
  252. {
  253.     char line[512];        /* line from route file */
  254.     register char *lp;        /* current line position */
  255.     char *routep;               /* ptr into route */
  256.     char *pattern;        /* pattern from line */
  257.     int  dom_ok;        /* in right domain */
  258.     int  host_ok;        /* right host */
  259.     FILE *rf;            /* route file */
  260.     char local_host[100];    /* callers host name */
  261.     char local_domain[100];    /* this domain */
  262.     
  263.     if (bang && *bang == NUL) bang = NULL;
  264.     if (remote_host == NULL || *remote_host == NUL) return 0;
  265.     
  266.     if (remote_domain && *remote_domain == NUL) remote_domain = NULL;
  267.     
  268.     gethostname(local_host, 100);
  269.     if (routep = strchr(local_host, '.')) *routep = NUL;
  270.     local_domain[0] = NUL;
  271.     
  272.     rf = open_file(relative(lib_directory, "routes"), OPEN_READ);
  273.     if (rf == NULL) {
  274. #ifdef TRACE
  275.     if (route_trace) fprintf(route_trace, "---No routes file\n");
  276. #endif
  277.     return 0;
  278.     }
  279.     
  280.     dom_ok = host_ok = 1;
  281.     routep = route;
  282.     pattern = NULL;
  283.     
  284.     while (fgets(line, 512, rf) != NULL) {
  285.     lp = line;
  286.     while (*lp && isspace(*lp)) lp++;
  287.     if (*lp == NUL || *lp == '#') continue;
  288.     
  289.     if (*lp == '/') {
  290.         lp++;
  291.         cmdc = *lp++;
  292.         while (*lp && isspace(*lp)) lp++;
  293.         if (*lp == '#') *lp = NUL;
  294.         
  295.         if (cmdc == 'L') {        /* local (default) domain name(s) */
  296.         cstrcpy(local_domain, lp);
  297.  
  298.         if (remote_domain == NULL ||
  299.             cstreq(lp, remote_domain) != NULL) {
  300.             dom_ok = 1;
  301.             if (strcmp(local_host, remote_host) == 0) {
  302.             pattern = "%p%n";
  303.             break;
  304.             }
  305.         }
  306.         continue;
  307.         }
  308.         
  309.         if (cmdc == 'D') {        /* destination domain */
  310.         if (*lp == NUL)
  311.             dom_ok = 1;
  312.         else
  313.             dom_ok = (cstreq(lp, remote_domain) != NULL);
  314.         continue;
  315.         }
  316.  
  317.         if (!dom_ok) continue;
  318.            
  319.         if (cmdc == 'H') {        /* local host */
  320.         if (*lp == NUL)
  321.             host_ok = 1;
  322.         else
  323.             host_ok = (cstreq(lp, local_host) != NULL);
  324.         continue;
  325.         }
  326.         
  327.         if (!host_ok) continue;
  328.  
  329.         switch (cmdc) {
  330.  
  331.          case 'N':    /* neighbouring (uucp) sites */
  332.         if (cstreq(lp, remote_host) == NULL) continue;
  333.         pattern = "%s!%n";
  334.         break;
  335.         
  336.          case 'P':
  337.         if (*lp == '+')
  338.             routep = cstrcpy(routep, ++lp);
  339.         else
  340.             routep = cstrcpy(route, lp);
  341.         continue;
  342.  
  343.          case 'G':
  344.         pattern = lp;
  345.         break;
  346.  
  347.          case 'B':
  348.         if (!bang) continue;
  349.         pattern = lp;
  350.         break;
  351.  
  352.          default:
  353.         continue;
  354.         }
  355.  
  356.         break;
  357.     }
  358.     
  359.     if (!dom_ok) continue;
  360.     if (!host_ok) continue;
  361.  
  362.     if ((pattern = cstreq(lp, remote_host))!=NULL) break;
  363.     }
  364.     
  365.     fclose(rf);
  366.     
  367.     if (pattern == NULL) return 0;
  368.   
  369. #ifdef TRACE  
  370.     if (route_trace) fprintf(route_trace, "   pattern='%s'\n", pattern);
  371. #endif
  372.     
  373.     for (; *pattern != NL && *pattern != NUL; pattern++) {
  374.     if (*pattern == SP || *pattern == TAB) continue;
  375.     if (*pattern == '%') {
  376.         pattern++;
  377.         switch(*pattern) {
  378.          case 'n':
  379.         routep = cstrcpy(routep, remote_user);
  380.         continue;
  381.          case 's':
  382.         routep = cstrcpy(routep, remote_host);
  383.         continue;
  384.          case 'd':
  385.         routep = cstrcpy(routep, 
  386.                  remote_domain ? remote_domain : local_domain);
  387.         continue;
  388.          case 'b':
  389.         routep = cstrcpy(routep, bang);
  390.         continue;
  391.          case 'p':
  392.         routep = route;
  393.         continue;
  394.          case '%':
  395.         break;
  396.          default:
  397.         continue;
  398.         }
  399.     }
  400.     *routep++ = *pattern;
  401.     }
  402.     *routep = NUL;
  403.     
  404.     return 1;
  405. }
  406.  
  407. #endif
  408.